package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a04_有效的括号复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/1/15
 */
public class a04_有效的括号复习3 {
    public boolean isValid(String s) {
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (char c : chars) {
            if(c == '('){
                stack.push(')');
            }else if(c == '['){
                stack.push(']');
            }else if(c == '{'){
                stack.push('}');
            }else if(stack.isEmpty()){
                return false;
            }else if(stack.peek() != c){
                return false;
            }else {
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}
